package cn.edu.nwsuaf.examcenter.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.nwsuaf.examcenter.pojo.College;
import cn.edu.nwsuaf.examcenter.pojo.Grade;
import cn.edu.nwsuaf.examcenter.pojo.Major;
import cn.edu.nwsuaf.examcenter.service.CollegeService;
import cn.edu.nwsuaf.examcenter.service.GradeService;
import cn.edu.nwsuaf.examcenter.service.MajorService;

/****
 * 年级控制器，控制年级数据的流向
 * @author Fantacy
 *
 */

@Controller
@RequestMapping("/grade")
public class GradeController {
	@Resource(name="gradeService")
	private GradeService gradeService;
	
	@Resource(name="collegeService")
	private CollegeService collegeService;
	
	@Resource(name="majorService")
	private MajorService majorService;
	
	/****
	 * 加载查询年级信息
	 * @param key
	 * @param p
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value="/loadGrade")
	public String loadGrade(@RequestParam(value="key", required=false)String key, @RequestParam(value="p", required=false)String p, ModelMap modelMap){
		gradeService.loadGrade(modelMap);
		return "exam/grade-list";
	}
	
	/****
	 * 加载学院信息
	 */
	@ResponseBody
	@RequestMapping(value="/json/loadCollege")
	public List<College> jsonLoadCollege(){
		return collegeService.jsonLoadCollege();
	}
	
	/****
	 * 加载专业信息
	 */
	@ResponseBody
	@RequestMapping(value="/json/loadMajor/{collegeNo}")
	public List<Major> jsonLoadMajor(@PathVariable(value="collegeNo")String collegeNo){
		return majorService.jsonLoadMajor(collegeNo);
	}
	
	/****
	 * 添加年级信息
	 * @param grade
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/addGrade",method=RequestMethod.POST)
	public String addGrade(@ModelAttribute Grade grade){
		return gradeService.addGrade(grade);
	}
	
	/***
	 * 单个删除年级数据
	 * @param gradeNo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteGrade/{gradeNo}")
	public String deleteGrade(@PathVariable("gradeNo")String gradeNo){
		return gradeService.deleteGrade(gradeNo);
	}
	
	@ResponseBody
	@RequestMapping(value="/deleteGradeList")
	public String deleteGradeList(@RequestParam("array")String array){
		return gradeService.deleteGradeList(array);
	}
}
